﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Willow.IRepository;
using Willow.IService;
using Willow.Model;
using Willow.Extensions;
using Willow.IService.Auth;
namespace Willow.Service.Auth
{
    public class ActionSerivce : BaseService<Auth_Action>, IActionService
    {
        IRepository<Auth_ModuleAction> ModuleActionRepository;
        public ActionSerivce(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
            ModuleActionRepository = unitOfWork.GetStandardRepository<Auth_ModuleAction>();
        }

        /// <summary>
        /// 提交功能
        /// </summary>
        /// <param name="lstInsert">The LST insert.</param>
        /// <param name="lstDelete">The LST delete.</param>
        /// <param name="lstUpdate">The LST update.</param>
        /// <param name="moduleId">The module identifier.</param>
        public void Submit(IEnumerable<Auth_Action> lstInsert, IEnumerable<Auth_Action> lstDelete, IEnumerable<Auth_Action> lstUpdate, Guid moduleId)
        {

            foreach (var item in lstInsert.OrEmptyIfNull())
            {
                item.Id = Guid.NewGuid();
                item.IsDelete = false;
                base.Create(item);

                //增加模块功能关联
                ModuleActionRepository.Create(new Auth_ModuleAction
                {
                    Id = Guid.NewGuid(),
                    ActionId = item.Id,
                    ModuleId = moduleId
                });
            }

            foreach (var item in lstDelete.OrEmptyIfNull())
            {
                var obj = base.Find(item.Id);
                obj.IsDelete = true;
                base.Update(obj);
                //删除模块功能关联
                var moduleAction = ModuleActionRepository.FirstOrDefault(p => p.ModuleId == moduleId && p.ActionId == item.Id);
                ModuleActionRepository.Delete(moduleAction);
            }

            foreach (var item in lstUpdate.OrEmptyIfNull())
            {
                base.Update(item);
            }

            UnitOfWork.SaveChanges();
        }
    }
}
